<?php
/**
 * @author jim
 * @date 2017/11/25
 */

namespace app\admin\controller;


use app\admin\model\Company;

class Citycompany extends Common
{
    public function index() {

        if (session('company_type') > 1) { // 市级及以下的用户不可进入
            // 无权进入
            $this->redirect('index/index');
        }

        $where = array();

        // 公司条件
        $whereComCondition = array();

        $whereStart = array();
        $whereEnd   = array();

        // 定义存储查询条件的数组
        $conditionArr = array();

        // 基本的查询条件
        if (session('province_id') > 0) {
            $where['com.ProvinceId'] = session('province_id');
            $whereComCondition['ProvinceId'] = session('province_id');
        }

        if (session('city_id') > 0) {
            $where['com.CityId'] = session('city_id');
            $whereComCondition['CityId'] = session('city_id');
        }

        if (session('county_id') > 0) {
            $where['com.CountyId'] = session('county_id');
            $whereComCondition['CountyId'] = session('county_id');
        }



        $where['com.CompanyTypeId'] = 2;
        $whereComCondition['CompanyTypeId'] = 2;

        // 列表查询条件
        if (input('sCityId') && input('sCityId')>0) {
            $conditionArr['sCityId'] = input('sCityId');
            $where['com.CityId'] = input('sCityId');
            $this->assign('sCityId',input('sCityId'));
        }

        if (input('sTrueName')) {
            $conditionArr['sTrueName'] = input('sTrueName');
            $where['c.TrueName'] = ['like',input('sTrueName').'%'];
            $this->assign('sTrueName',input('sTrueName'));
        }

        if (input('sAddress')) {
            $conditionArr['sAddress'] = input('sAddress');
            $where['com.address'] = ['like',input('sAddress').'%'];
            $this->assign('sAddress',input('sAddress'));
        }

        $this->assign('start',input('start'));
        $this->assign('end',input('end'));
        if (input('start')) {
            $conditionArr['start'] = input('start');
            $whereStart['com.CreateOn'] = ['>',input('start').' 00:00'];
        }

        if (input('end')) {
            $conditionArr['end'] = input('end');
            $whereEnd['com.CreateOn'] = ['<',input('end').' 23:59'];
        }


        $pagesize = config('default_page_size');
        $res = $this->db_app->table('company')
            ->alias('com')
            ->join('customer c','com.CustomerId = c.Id','LEFT')
            ->where($where)
            ->where($whereStart)
            ->where($whereEnd)
            ->order('com.Money desc')
            ->field('com.Id,com.CompanyName,com.address,com.Money,com.LeftMoney,com.CreateOn,c.TrueName,com.ProvinceId,com.CityId,com.CountyId,com.TownId')
            ->paginate($pagesize,false,[
            'query' => $conditionArr
            ]); // 防止参数丢失


        $page = $res->render(); // 分页
        $data = $res->all(); // 数据

//        dump($data);

        $this->assign('page',$page);
        $this->assign('data',$data);


        // 获取相关的公司列表
        $companyModel = new Company();
        $companyData = $companyModel->selectData($whereComCondition,'CompanyName,CityId');
        $this->assign('companyData',$companyData);
//        dump($companyData);


        return $this->fetch();
    }


}